package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.bocai.eelly.R;
import com.bocai.eelly.bean.PopBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.fragments.FindClothingStockFragment;
import com.bocai.eelly.fragments.FindFuliaoStockFragment;
import com.bocai.eelly.fragments.FindMianliaoStockFragment;
import com.bocai.eelly.fragments.FindToolsStockFragment;
import com.bocai.eelly.utils.DeviceUtil;
import com.bocai.eelly.utils.PopUtil;
import com.bocai.eelly.views.ExpandTabView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class FindStockActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.search)
    RelativeLayout search;
    @Bind(R.id.ll_select_stock)
    LinearLayout llSelectStock;
    @Bind(R.id.fl_find_stock)
    FrameLayout flFindStock;
    Fragment findClothingStockFrg, findMianliaoStockFrg, findFuliaoStockFrg, findToolsStockFrg;
    @Bind(R.id.toggleBtn)
    ToggleButton toggleBtn;
    private PopUtil pu;
    private ToggleButton selectedButton;
    private ExpandTabView.OnButtonClickListener mOnButtonClickListener;
    private int stock=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_stock);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        search.setOnClickListener(this);
        llSelectStock.setOnClickListener(this);
        toggleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToggleButton tButton = (ToggleButton) view;

                if (selectedButton != null && selectedButton != tButton) {
                    selectedButton.setChecked(false);
                }
                int width = new DeviceUtil(FindStockActivity.this).getWidth();
                showPopwindow(llSelectStock, setSortList(), width, 20);
                selectedButton = tButton;
                if (mOnButtonClickListener != null && tButton.isChecked()) {
                    mOnButtonClickListener.onClick();
                }
            }
        });
    }

    private void initView() {
        setSelect(0);
        ToolbarHelper.setup(this, R.string.str_title_find_stock, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    String[] types = {"0", "1", "2", "3"};
    String[] typeTitels = {"库存服装", "库存面料", "库存辅料", "库存设备"};

    private ArrayList<PopBean> setSortList() {
        ArrayList list = new ArrayList<>();
        for (int i = 0; i < types.length; i++) {
            PopBean popBean = new PopBean();
            popBean.setId(types[i]);
            popBean.setName(typeTitels[i]);

            list.add(i, popBean);
        }

        return list;
    }

    private void showPopwindow(View v,  ArrayList<PopBean> list, int width, int height) {

        pu = PopUtil.getInstance(this, v,toggleBtn, width, height, list);
        pu.setListener(new PopUtil.OnDatePopClickListener() {
            @Override
            public void onClick(PopUtil pu, PopBean date) {
                toggleBtn.setText(date.getName());
                setSelect(Integer.parseInt(date.getId()));
                stock=Integer.parseInt(date.getId());
                pu.dismiss();
            }
        });
        pu.show();
    }

    public void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        switch (i) {
            case 0:
                hideFragment(transaction);
                if (findClothingStockFrg == null) {
                    findClothingStockFrg = new FindClothingStockFragment();
                    transaction.add(R.id.fl_find_stock, findClothingStockFrg);
                } else {
                    transaction.show(findClothingStockFrg);
                }

                break;
            case 1:
                hideFragment(transaction);
                if (findMianliaoStockFrg == null) {
                    findMianliaoStockFrg = new FindMianliaoStockFragment();
                    transaction.add(R.id.fl_find_stock, findMianliaoStockFrg);
                } else {
                    transaction.show(findMianliaoStockFrg);
                }

                break;
            case 2:
                hideFragment(transaction);
                if (findFuliaoStockFrg == null) {
                    findFuliaoStockFrg = new FindFuliaoStockFragment();
                    transaction.add(R.id.fl_find_stock, findFuliaoStockFrg);
                } else {
                    transaction.show(findFuliaoStockFrg);
                }

                break;
            case 3:
                hideFragment(transaction);
                if (findToolsStockFrg == null) {
                    findToolsStockFrg = new FindToolsStockFragment();
                    transaction.add(R.id.fl_find_stock, findToolsStockFrg);
                } else {
                    transaction.show(findToolsStockFrg);
                }

                break;

        }
        transaction.commitAllowingStateLoss();
    }


    private void hideFragment(FragmentTransaction transaction) {
        if (findClothingStockFrg != null) {
            transaction.hide(findClothingStockFrg);
        }
        if (findMianliaoStockFrg != null) {
            transaction.hide(findMianliaoStockFrg);
        }
        if (findFuliaoStockFrg != null) {
            transaction.hide(findFuliaoStockFrg);
        }
        if (findToolsStockFrg != null) {
            transaction.hide(findToolsStockFrg);
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_select_stock:
                int width = new DeviceUtil(this).getWidth();
                showPopwindow(llSelectStock, setSortList(), width, 20);
                break;
            case R.id.search:
                Intent intent=new Intent(this,SearchStockActivity.class);
                intent.putExtra("stock",stock);
                startActivity(intent);
                break;
        }
    }
}
